import { v4 as uuidv4 } from 'uuid';

export default {
  use(app, knex){

    //新增用户
    app.post('/user/add', function(req, res){
      knex('user')
      .where('user', '=', req.body.user)
      .then(rows => {
        if(rows.length > 0){
          res.send({
            code: 1,
            message: `用户名${req.body.user}已存在, 请修改后重新提交`
          })
        }else{
          knex('user')
          .insert({id: uuidv4(), ...req.body})
          .then(row=>{
            res.send({
              code: 0,
              message: '新增用户成功'
            })
          })
          .catch(err=>{
            console.log(err)
            res.send({
              code: 1,
              message: err
            })
          })  
        }
      })
    })


    //查询用户
    app.get('/user', function(req, res){
      knex('user')
      .select('*').then(rows=>{
        res.send({
          code: 0,
          data: rows
        })
      })  
      .catch(err => {
        console.log(err)
        res.send({
          code: 1,
          message: err
        })
      })
    })

    
    //删除用户
    app.post('/user/del', function(req, res){
      knex('user')
      .where('id', req.body.id)
      .del()
      .then(row => {
        res.send({
          code: 0,
          data: '用户删除成功'
        })
      })
      .catch(err => {
        res.send({
          code: 1,
          message: err
        })
      })
    })

    
    //更新用户
    app.post('/user/update', function(req, res){
      const data = req.body;
      const id = data.id;
      delete data.id;
      knex('user')
      .where('id', '=', id)
      .update({
        ...data
      })
      .then(row => {
        res.send({
          code: 0,
          data: '更新用户成功'
        })
      })
      .catch(err => {
        res.send({
          code: 1,
          message: err
        })
      })
    })

    app.post('/user/deviceTag', function(req, res){
      if(!req.body.user_id){
        res.send({
          code: 0,
          data: []
        })
        return;
      }
      const {page, size} = req.query;
      knex('user_tag')
      .where('user_id', '=', req.body.user_id)
      .limit(size)
      .offset((page-1) * size)
      .select('*')
      .then( rows =>{

        knex('user_tag')
        .count('user_id as total')
        .where('user_id', '=', req.body.user_id)
        .then( counts => {
          res.send({
            code: 0,
            data: rows,
            total: counts[0].total
          })
        })
       
      })
      .catch(err => {
        console.log(err)
        res.send({
          code: 1,
          message: err 
        })
      })
    })

    app.post('/user/bindDeviceTag', function(req, res){
      const data = req.body.map(item =>{
        item.id = item.user_id + item.tag_id;
        return item;
      })
      knex('user_tag')
      .insert(data)
      .onConflict('id')
      .ignore()
      .then(()=>{
        res.send({
          code: 0,
          message: '绑定成功'
        })
      
      })
    })

    app.post('/user/unbindDeviceTag', function(req, res){
      const tags = req.body.map(item => {return item.tag_id})
      const users = req.body.map(item => {return item.user_id})
      knex('user_tag')
      .whereIn('tag_id', tags)
      .whereIn('user_id', users)
      .del()
      .then(()=>{
        res.send({
          code: 0,
          message: '解绑成功'
        })
      
      })
    })
  }
}

